package com.cblz.bd.server.core.handle;

import cn.dev33.satoken.exception.NotLoginException;
import com.cblz.bd.common.core.constant.ResponseStatusCode;
import com.cblz.bd.common.core.handle.BaseExceptionHandler;
import com.cblz.bd.common.core.utils.ResultResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpStatus;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.Map;

/**
 * 自定义异常处理器
 */
@Slf4j
@RestControllerAdvice
@ControllerAdvice
@Order(value = Ordered.HIGHEST_PRECEDENCE)
public class GlobalExceptionHandler extends BaseExceptionHandler {

    /**
     * 登录认证异常处理
     *
     * @param notLoginException
     * @return
     */
    @ExceptionHandler(NotLoginException.class)
    public ResultResponse handleNotLoginException(NotLoginException notLoginException) {
        log.error(notLoginException.getMessage());
        return new ResultResponse().message(ResponseStatusCode.NOT_LOGIN.getStatusCode(), "登录信息失效，请重新登录", Map.of("routePath", "/"));
    }

    /**
     * HttpRequestMethodNotSupportedException
     *
     * @param exception
     * @return
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseStatus(HttpStatus.NOT_FOUND)
    public ResultResponse handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) {
        log.error(exception.getMessage());
        return new ResultResponse().message(ResponseStatusCode.ERROR.getStatusCode(), "请求路径不存在或请求方式错误");
    }
}